package com.enbatis.mybatisplugs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.NewsData;
import com.enbatis.mybatisplugs.entity.vo.WxSelectionVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.toolkit.HttpRequestUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.WxSelection;
import com.enbatis.mybatisplugs.service.WxSelectionService;


import java.util.*;

@RestController
@RequestMapping("v1/wx_selection")
public class WxSelectionController extends BaseController {

@Autowired
private WxSelectionService wxSelectionService;


    @PostMapping("/add")
    public ResultReturn<WxSelection> add(@RequestBody WxSelection wxSelection){
        return  success(wxSelectionService.insert(wxSelection));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(wxSelectionService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody WxSelection wxSelection){
        wxSelection.setId(id);
        return success(wxSelectionService.updateById(wxSelection));
    }


    @GetMapping("/{id}")
    public ResultReturn<WxSelection> get(@PathVariable("id") Long id) {
        return success(wxSelectionService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<WxSelection>> getList(WxSelection wxSelection){
        return success(wxSelectionService.list(new Wrapper<>(wxSelection)));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<WxSelection>> getPage(WxSelection wxSelection){
        Map<String,String> map= new HashMap<>();
        map.put("key","660781b6e6b7900d67865cbcceb1541e");
        JSONObject jsonObject=  HttpRequestUtils.httpGet("http://v.juhe.cn/weixin/query",map);

        if (null==jsonObject){
            return success(wxSelectionService.page(getPage(),new Wrapper<>(wxSelection).orderBy("ctime",false)));
        }
        JSONObject result=  (JSONObject)jsonObject.get("result");
        if (null==result){
            return success(wxSelectionService.page(getPage(),new Wrapper<>(wxSelection).orderBy("ctime",false)));
        }
        JSONArray jsonArray=  (JSONArray)result.get("list");
        if (null==jsonArray){
            return success(wxSelectionService.page(getPage(),new Wrapper<>(wxSelection).orderBy("ctime",false)));
        }
        List<WxSelectionVO> newsDataList= jsonArray.toJavaList(WxSelectionVO.class);

        if (CollectionUtils.isEmpty(newsDataList)){
            return success(wxSelectionService.page(getPage(),new Wrapper<>(wxSelection).orderBy("ctime",false)));
        }
        List<WxSelection> dataList=wxSelectionService.list(new Wrapper<>(wxSelection).setSqlSelect("business_id"));
        Map<String,String> uniquekeyMap=new HashMap<>();
        for (WxSelection data:dataList) {
            uniquekeyMap.put(data.getBusinessId(),data.getBusinessId());
        }

        List<WxSelection> saveList=new ArrayList<>();

        for (WxSelectionVO data:newsDataList) {
            if (uniquekeyMap.get(data.getId())==null){
                WxSelection selection=new WxSelection();
                selection.setBusinessId(data.getId());
                selection.setTitle(data.getTitle());
                selection.setFirstimg(data.getFirstimg());
                selection.setMark(data.getMark());
                selection.setSource(data.getSource());
                selection.setUrl(data.getUrl());
                selection.setCtime(new Date());
                saveList.add(selection);
            }
        }
        if (CollectionUtils.isNotEmpty(saveList)){
            wxSelectionService.saveBatch(saveList);
        }

        return success(wxSelectionService.page(getPage(),new Wrapper<>(wxSelection).orderBy("ctime",false)));
    }

}
